package creational.singleton;

/**
 * @ClassName: StaticInnerClassSingleton
 * @Description: 静态内部类
 * @Author: liu-hao
 * @Date: 2023-03-27 21:44
 * @Version: 1.0
 **/
public class StaticInnerClassSingleton {

    private StaticInnerClassSingleton() {
        // 防止通过反射进行实例化（注意，此方式不严谨，反射可以修改单例对象引用）
        if (HelperHolder.INSTANCE != null) {
            throw new IllegalStateException("单例对象已经初始化！");
        }
    }

    public static StaticInnerClassSingleton getInstance() {
        return HelperHolder.INSTANCE;
    }

    /**
     * 持有单例对象的静态内部类
     */
    private static class HelperHolder {
        // 此final可写可不写，策略问题，写上最好
        private static final StaticInnerClassSingleton INSTANCE = new StaticInnerClassSingleton();
    }
}
